import os
from git.exc import GitCommandError
from git.repo import Repo
import json

RED_OP = "\033[31m{}\033[0m"
GREEN_OP = "\033[32m{}\033[0m"
YELLOW_OP = "\033[33m{}\033[0m"
BLUE_OP = "\033[34m{}\033[0m"
DEFAULT_OP = "\033[38m{}\033[0m"
space = " "
root_fd = os.path.join(os.path.expanduser("~"), "temp", "yapai")
error_pro = {}
for path, dir_list, file_list in os.walk(
    os.path.join(os.path.expanduser("~"), "shell/com/yapai/project")
):
    for file_name in file_list:
        with open(os.path.join(path, file_name), "r") as load_file:
            proJson = json.load(load_file)
            sub_fd_name = proJson["project_name"]
            branch_name = proJson["branch_name"]
            main_branch = proJson["main_branch"]
            project_fd = os.path.join(root_fd, sub_fd_name)
            if os.path.exists(project_fd) is False:
                continue
            print(YELLOW_OP.format(sub_fd_name))
            repo = Repo(project_fd)
            diffLine = ""
            # diffLine = repo.git.diff(
            #     repo.active_branch, "origin/{}".format(repo.active_branch)
            # )
            if repo.is_dirty() is True or len(diffLine) > 0:
                repo.git.reset("--hard", "origin/{}".format(repo.active_branch))
            repo.git.checkout(main_branch)
            repo.git.pull()
            pro_error = []
            if "unit_branches" in proJson:
                branches = proJson["unit_branches"]
                for b in branches:
                    repo.git.checkout(b)
                    # need update again
                    repo.git.pull()
                    try:
                        repo.git.merge(main_branch)
                        if repo.is_dirty() is True:
                            print(space * 4, RED_OP.format(b))
                        else:
                            print(space * 4, GREEN_OP.format(b))
                            repo.git.push("--progress", "origin", b)
                    except GitCommandError as be:
                        print(be)
                        repo.git.reset(
                            "--hard", "origin/{bran}".format(bran=repo.active_branch)
                        )
                        pro_error.append(b)
                        # for i in range(0, 3):
                        #     print(
                        #         GREEN_OP.format(sub_fd_name)
                        #         + RED_OP.format(
                        #             " found merge error ,  Please handler just now..."
                        #         )
                        #     )
                if len(pro_error) > 0:
                    error_pro[sub_fd_name] = pro_error
                repo.git.checkout(branch_name)
if len(error_pro) > 0:
    print("Next is a summary of the full report....")
    print("################################################################")
    for key in error_pro:
        print(RED_OP.format(key))
        for branch in error_pro[key]:
            print(space * 4, branch)
    print("################################################################")
